package ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.service.imp;

import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.devicecontroller.DeviceManager;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.devicecontroller.DeviceState;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.devicecontroller.command.BitCommand;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.devicecontroller.command.Command;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.model.SimpleCommand;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.service.SimpleCommandService;

public class SimpleCommandServiceImp implements SimpleCommandService {

	private BitCommand cmd;
	private SimpleCommand smpcmd;
	private DeviceManager devman;
	
	@Override
	public String getName() {
		return smpcmd.getName();
	}

	@Override
	public synchronized void setOn() {
		if(cmd.getState() == Command.WAITING || cmd.getState() == Command.PROCESSING )
			return;
		if(devman.getState() != DeviceState.CONNECTED){
			cmd.setState(Command.FAIL);
			return;
		}
		cmd.setBit(true);
		devman.execute(cmd);

	}

	@Override
	public synchronized void setOff() {
		if(cmd.getState() == Command.WAITING || cmd.getState() == Command.PROCESSING )
			return;
		if(devman.getState() != DeviceState.CONNECTED){
			cmd.setState(Command.FAIL);
			return;
		}
		cmd.setBit(false);
		devman.execute(cmd);
	}

	@Override
	public int getState() {
		return cmd.getState();
	}

	/**
	 * @return the cmd
	 */
	public BitCommand getCmd() {
		return cmd;
	}

	/**
	 * @param cmd the cmd to set
	 */
	public void setCmd(BitCommand cmd) {
		this.cmd = cmd;
	}

	/**
	 * @return the smpcmd
	 */
	public SimpleCommand getSmpcmd() {
		return smpcmd;
	}

	/**
	 * @param smpcmd the smpcmd to set
	 */
	public void setSmpcmd(SimpleCommand smpcmd) {
		this.smpcmd = smpcmd;
	}

	/**
	 * @return the devman
	 */
	public DeviceManager getDevman() {
		return devman;
	}

	/**
	 * @param devman the devman to set
	 */
	public void setDevman(DeviceManager devman) {
		this.devman = devman;
	}

}
